package org.thraex.excel;

import io.netty.channel.ChannelOption;
import org.junit.Test;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;
import org.thraex.excel.utils.ExportExcelOrgUtil;
import org.thraex.excel.vo.DetailVO;
import reactor.core.publisher.Mono;
import reactor.netty.http.client.HttpClient;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author 鬼王
 * @Date 2019/08/12 09:57
 */
public class ExcelExportForPOI {

    private WebClient webClient() {
        HttpClient httpClient = HttpClient.create().tcpConfiguration(client -> client
                .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000)
                .option(ChannelOption.SO_TIMEOUT, 5000));
        return WebClient.builder()
                .baseUrl("http://10.122.163.102:8888")
                .clientConnector(new ReactorClientHttpConnector(httpClient))
                .build();
    }

    @Test
    public void test() {
        Mono<Map> mapMono = this.webClient()
                .get()
                .uri("/attendance/main/query/export/org")
                .retrieve()
                .bodyToMono(Map.class);
        Map block = mapMono.block();

        List<Map> details = (List<Map>) block.get("data");
        List<DetailVO> collect = details.stream().map(it -> new DetailVO(
                it.get("userId") == null ? null : it.get("userId").toString(),
                it.get("userName") == null ? null : it.get("userName").toString(),
                it.get("month") == null ? null : it.get("month").toString(),
                it.get("deptName") == null ? null : it.get("deptName").toString(),
                it.get("remark") == null ? null : it.get("remark").toString(),
                Integer.valueOf(it.get("countLY").toString()),
                Integer.valueOf(it.get("countYB").toString()),
                Integer.valueOf(it.get("countCJ").toString()),
                Integer.valueOf(it.get("countHJ").toString()),
                Integer.valueOf(it.get("countCQ").toString()),
                Integer.valueOf(it.get("countBJ").toString()),
                Integer.valueOf(it.get("countSANGJ").toString()),
                Integer.valueOf(it.get("countT").toString()),
                Integer.valueOf(it.get("countBX").toString()),
                Integer.valueOf(it.get("countJB").toString()),
                Integer.valueOf(it.get("countTQJ").toString()),
                Integer.valueOf(it.get("countDXXJ").toString()),
                Integer.valueOf(it.get("countWJ").toString()),
                Integer.valueOf(it.get("countGX").toString()),
                Integer.valueOf(it.get("countKGJ").toString()),
                Integer.valueOf(it.get("countGSJ").toString()),
                Integer.valueOf(it.get("countCC").toString()),
                Integer.valueOf(it.get("countSJ").toString()),
                it.get("empNo") == null ? null : it.get("empNo").toString(),
                it.get("empType") == null ? null : it.get("empType").toString(),
                it.get("day1") == null ? null : it.get("day1").toString(),
                it.get("day2") == null ? null : it.get("day2").toString(),
                it.get("day3") == null ? null : it.get("day3").toString(),
                it.get("day4") == null ? null : it.get("day4").toString(),
                it.get("day5") == null ? null : it.get("day5").toString(),
                it.get("day6") == null ? null : it.get("day6").toString(),
                it.get("day7") == null ? null : it.get("day7").toString(),
                it.get("day8") == null ? null : it.get("day8").toString(),
                it.get("day9") == null ? null : it.get("day9").toString(),
                it.get("day10") == null ? null : it.get("day10").toString(),
                it.get("day11") == null ? null : it.get("day11").toString(),
                it.get("day12") == null ? null : it.get("day12").toString(),
                it.get("day13") == null ? null : it.get("day13").toString(),
                it.get("day14") == null ? null : it.get("day14").toString(),
                it.get("day15") == null ? null : it.get("day15").toString(),
                it.get("day16") == null ? null : it.get("day16").toString(),
                it.get("day17") == null ? null : it.get("day17").toString(),
                it.get("day18") == null ? null : it.get("day18").toString(),
                it.get("day19") == null ? null : it.get("day19").toString(),
                it.get("day20") == null ? null : it.get("day20").toString(),
                it.get("day21") == null ? null : it.get("day21").toString(),
                it.get("day22") == null ? null : it.get("day22").toString(),
                it.get("day23") == null ? null : it.get("day23").toString(),
                it.get("day24") == null ? null : it.get("day24").toString(),
                it.get("day25") == null ? null : it.get("day25").toString(),
                it.get("day26") == null ? null : it.get("day26").toString(),
                it.get("day27") == null ? null : it.get("day27").toString(),
                it.get("day28") == null ? null : it.get("day28").toString(),
                it.get("day29") == null ? null : it.get("day29").toString(),
                it.get("day30") == null ? null : it.get("day30").toString(),
                it.get("day31") == null ? null : it.get("day31").toString()
        )).collect(Collectors.toList());

        ExportExcelOrgUtil.zxExprotExcelXLSX(collect, "2019-06");
    }

}
